//
//  WJ_MeFollow_VC.m
//  WJBSApp
//
//  Created by dllo on 16/7/16.
//  Copyright © 2016年 王景伟. All rights reserved.
//

//  这是我发表的文章
#import "WJ_MeFollow_VC.h"
#import "WJBaseTableViewCell.h"
#import "WJDataManager.h"
#import "WJPublishTextCell.h"
#import "WJPublishLinksCell.h"
#import "WJPublishPicCell.h"
#import "WJPublishModel.h"
#import "WJPublishAudioCell.h"
#import "WJVideoCell.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>


@interface WJ_MeFollow_VC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) WJBaseTableView *tableView;

@property (nonatomic, strong) NSMutableArray *followArray;

@property(nonatomic,strong)AVPlayer * player;

@property(nonatomic,strong)AVPlayerViewController * playerView;

@end

@implementation WJ_MeFollow_VC
-(void)dealloc
{
    [_tableView release];
    [_followArray release];
    [_player release];
    [_playerView release];
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];
    
    [self createData];
    
}

- (void)createView
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的发表";
    
    self.tableView = [[WJBaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[WJPublishTextCell class] forCellReuseIdentifier:@"publishCell"];
    [self.tableView registerClass:[WJPublishLinksCell class] forCellReuseIdentifier:@"publishLinksCell"];
    [self.tableView registerClass:[WJPublishPicCell class] forCellReuseIdentifier:@"publishPicCell"];
    [self.tableView registerClass:[WJVideoCell class] forCellReuseIdentifier:@"publishVideoCell"];
    [self.tableView registerClass:[WJPublishAudioCell class] forCellReuseIdentifier:@"publishAudioCell"];
    [_tableView release];
    
}

- (void)createData
{
    self.followArray = [[WJDataManager shareFMDB]selectAllPublish];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.followArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WJPublishModel *model = self.followArray[indexPath.row];

    if ([model.type isEqualToString:@"links"]) {
        
        WJPublishLinksCell *cell = [tableView dequeueReusableCellWithIdentifier:@"publishLinksCell"];
        
        cell.model = model;
        
        cell.Block = ^(NSString *nowTime){
           
            [[WJDataManager shareFMDB]deleteFromText:nowTime];
            
             self.followArray = [[WJDataManager shareFMDB]selectAllPublish];
            
            [self.tableView reloadData];
        };
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
    if ([model.type isEqualToString:@"pic"]) {
        
        WJPublishPicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"publishPicCell"];
        
        cell.model = model;
        cell.Block = ^(NSString *nowTime){
            
            [[WJDataManager shareFMDB]deleteFromText:nowTime];
            
             self.followArray = [[WJDataManager shareFMDB]selectAllPublish];
            
            [self.tableView reloadData];
        };
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
     }
    
    if ([model.type isEqualToString:@"video"]) {
        
        WJVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"publishVideoCell"];
        
        cell.model = model;
        
        cell.Block = ^(NSString *nowTime){
            
            [[WJDataManager shareFMDB]deleteFromText:nowTime];
            
            self.followArray = [[WJDataManager shareFMDB]selectAllPublish];
            
            [self.tableView reloadData];
        };
        
        cell.VideoBlock = ^(NSString *videoPath){
            
            //  播放
            NSURL *url=[NSURL fileURLWithPath:videoPath];
            AVPlayer *player=[AVPlayer playerWithURL:url];
            AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
            playerLayer.frame=  cell.picImageView.bounds;
            [cell.picImageView.layer addSublayer:playerLayer];
            [player play];
  
        };
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
        return cell;
    }
    
    if ([model.type isEqualToString:@"audio"]) {
        
        WJPublishAudioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"publishAudioCell"];
        
        cell.model = model;
        
        cell.Block = ^(NSString *nowTime){
            
            [[WJDataManager shareFMDB]deleteFromText:nowTime];
            
            self.followArray = [[WJDataManager shareFMDB]selectAllPublish];
            
            [self.tableView reloadData];
        };
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }

    
    WJPublishTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"publishCell"];
    
    cell.model = model;
    cell.Block = ^(NSString *nowTime){
        
        [[WJDataManager shareFMDB]deleteFromText:nowTime];
        
         self.followArray = [[WJDataManager shareFMDB]selectAllPublish];
        
        [self.tableView reloadData];
    };
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WJPublishModel *model = self.followArray[indexPath.row];
    return model.cellHeight;
    
}















































































































































































































































































































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
